package datalookin.network;

import android.util.Log;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.whu.zj.MyApplication;
import com.whu.zj.R;
import com.whu.zj.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import inter.Delegate;

/**
 * 获取历史管网信息
 * Created by Administrator on 2016/3/7.
 */
public class GetHistoryWaterInfo {
    Delegate delegate;
    StringRequest stringRequest;

    String beginTime;
    String endTime;
    String loc;
    String type;

    public GetHistoryWaterInfo(String beginTime, Delegate delegate, String endTime, String loc, String type) {
        this.beginTime = beginTime;
        this.delegate = delegate;
        this.endTime = endTime;
        this.loc = loc;
        this.type = type;
    }

    public GetHistoryWaterInfo() {
    }

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void setBeginTime(String beginTime) {
        this.beginTime = beginTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void get() throws UnsupportedEncodingException {
        String ip = MyApplication.share().getResources().getString(R.string.IP_zj);
        String url = "http://" + ip +"/GetHistoryDataByIdType?id="+loc+"&type="+URLEncoder.encode(type,"utf-8")+"&beginTime="+beginTime+"&endTime="+endTime;
        stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("管网历史数据", response);
                if (delegate != null)
                    delegate.onResponse(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error);
            }
        });
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    public void cancel()
    {
        if (!stringRequest.isCanceled())
            stringRequest.cancel();
    }
}
